require '../db_help/db_help'
require 'taobao_api'
require 'net/http'
require 'open-uri'
require 'iconv'
class GetData
      FLITER=[21541,1625894,1625894,20509,1625899,1625894,20518,20509,1627207,31610]
      def set_cat_info(item,cat,parent)
        while p = parent.fetch_hash do
          item[cat+'_id'] = p['t_cid']
          item[cat+'_name'] = p['cat_name']
        end
      end
      def replace(compare1,compare2, r)
        puts compare2
        if !compare2.nil?
          index =  compare2.split(",").index(compare1)
          if !index.nil?
            r.split(",")[index]
          else
            compare1
          end
        else
          compare1
        end
      end
      def hash_to_str (h)
        val = ''
        h.each do |p|
          val += p.join(":")+";"
        end
        val.slice(0,val.length-1)
      end
      def transfer_prop_to_str_v2(dao,p)
          str={}
          cid = p['cat1_id']
          cid = p['cat2_id'] unless p['cat2_id'].nil?
          return if cid.nil?
          #pid_str = p['props'].to_s
          #FLITER.each do |ff|
           # a=Regexp.new("(#{ff}:\\d+;)")
            #pid_str.gsub!(a,'') unless pid_str.gsub!(a,'').nil?
          #end
          prop = p['props'].to_s.split(";")
          prop.each do |s|
            values = s.split(":")
            next unless FLITER.index(values[0].to_i).nil?
            rs = dao.query("select pid,property_name,name from tao_property_values where pid=#{values[0]} and vid=#{values[1]} and cid=#{cid}")
            if rs.num_rows>0
              while rst = rs.fetch_hash do
                s = rst['property_name']
                #replace(rst['property_name'],p['input_pids'],p['input_str'])
                if str[s].nil?
                  str[s]=rst['name']
                else
                  str[s]=str[s]+","+rst['name']
                end
              end
            end
          end
          return hash_to_str(str)
      end
      def escape(str)
          return nil if str.nil?
          str = str.gsub!(/"/,"'") unless str.gsub!(/"/,"'").nil?
          return str
      end
      def get_shop(nick,dao)
              begin
                shops = TaobaoAPI.get_with(:method=>'taobao.shop.get',
                                 :fields=>'sid,cid,nick,title,desc,bulletin,pic_path,created,modified',
                                 :nick=>nick)
                unless shops['shop_get_response'].nil?
                  shop = shops['shop_get_response']['shop']
                  unless dao.query("select id from tao_shops where t_sid='#{shop['sid']}'").num_rows>0
                    shop['t_sid']=shop['sid']
                    escape(shop['desc'])
                    shop['description']= shop['desc']
                    escape(shop['bulletin'])
                    shop['pic_path']="http://logo.taobao.com/shop-logo"+shop['pic_path'].to_s

                    shop.delete('desc')
                    shop.delete('sid')
                    shop['created_at']=TaobaoAPI.timestamp
                    shop['updated_at']=TaobaoAPI.timestamp
                    shop['table']='tao_shops'
                    shop['status']=0
                    shop.delete('modified')
                    shop.delete('remain_count')
                    shop.delete('shop_score')
                    dao.query(dao.build_sql(shop))
                    taobaoke_shop_convert(shop['t_sid'],dao)
                  end
                end
              rescue Exception => e
                raise e
              end
      end
      def taobaoke_shop_convert(sid,dao)
              begin
                   taobaoke_shops = TaobaoAPI.get_with(:method=>'taobao.taobaoke.shops.convert',
                                          :sids=>sid,
                                          :fields=>'user_id,shop_title,click_url,commission_rate',
                                          :nick=>'kqy929')
                 unless taobaoke_shops['taobaoke_shops_convert_response'].nil?

                      shop = taobaoke_shops['taobaoke_shops_convert_response']['taobaoke_shops']['taobaoke_shop']
                      shop['t_user_id']=shop['user_id']
                      escape(shop['shop_title'])
                      shop['status']=0
                      shop['table']='taoke_shops'
                      shop['created_at']=TaobaoAPI.timestamp
                      shop['updated_at']=TaobaoAPI.timestamp
                      shop.delete('user_id')
                    unless dao.query("select id from taoke_shops where t_user_id=#{shop['t_user_id']}").num_rows>0
                      dao.query(dao.build_sql(shop))
                    end
                  end
              rescue Exception => e
                  raise e
              end
            end
      def get_picture_from_description(dao,desc,num_iid)
        begin
          Net::HTTP.version_1_2
          imgs = desc.scan(/<img\s+[^>]*\s*src\s*=\s*[\'\"](\S+)[\'\"][^>]*>/)
          imgs.each do |url|
          begin
            next if url.to_s.scan('amos1.taobao.com').size>0
            rs = dao.query("select id from tao_items where num_iid=#{num_iid}")
            next unless rs.num_rows>0
            item_id = rs.fetch_row[0]
            url = url.to_s
            url.slice(0,url.index('>')) unless url.index('>').nil?
            next if dao.query("select id from tao_item_pictures where url='#{url}' and item_id=#{item_id}").num_rows>0
            url = Iconv.iconv("UTF-8//IGNORE","GBK//IGNORE",url).to_s
           	data=open(url)
           	if(data.size > 51200)
           	  dao.query("insert into tao_item_pictures(url,position,item_id,size,created_at,updated_at,status) values('#{url}',0,#{item_id},#{data.size},'#{TaobaoAPI.timestamp}','#{TaobaoAPI.timestamp}',0)")
           	  data.close(true)
           	end
          rescue Exception => e
          end
        end
        rescue Exception =>e
          raise e
        end 
      end
      #获取商品数据
      def get_item(dao,id,nick,spider_id)
                  begin
                items = TaobaoAPI.get_with(:method=>'taobao.item.get',
                                          :fields=>'cid,iid,detail_url,location,num_iid,title,nick,seller_cids,desc,props,'+
                                          'input_pids,input_str,pic_url,num,valid_thru,list_time,delist_time,price,'+
                                          'auction_point,property_alias,item_imgs',
                                          :nick=>nick,
                                          :num_iid=>id)
                item = items['item_get_response']['item'] unless items['item_get_response'].nil?
                unless item.nil?
                  escape(item['desc'])
                  escape(item['title'])
                  item['description']= item['desc']
                  item['table']='tao_items'
                  item['created_at']=TaobaoAPI.timestamp
                  item['updated_at']=TaobaoAPI.timestamp
                  item['location'] = item['location']['city']
                  item['status']=0

                  item.delete('desc')
                  unless item['description'].nil?
                   item['description']=item['description'].gsub!(/<table.*?(title=['"]\S+['"]).*?>.*?<\/table>/, '')  unless item['description'].gsub!(/<table.*?(title=['"]\S+['"]).*?>.*?<\/table>/, '').nil?
                  end
                  cats = dao.query("select cat_name,t_cid,cat_level,parent_id from tao_categories where t_cid=#{item['cid']} limit 1")

                  while category = cats.fetch_hash do
                    if category['cat_level']=='0'
                      item['cat1_id'] = category['t_cid']
                      item['cat1_name'] = category['cat_name']
                    elsif category['cat_level']=='1'
                      parent = dao.query("select cat_name,t_cid,cat_level,parent_id from tao_categories where t_cid=#{category['parent_id']}")
                      set_cat_info(item,'cat1',parent)
                      item['cat2_id'] = category['t_cid']
                      item['cat2_name'] = category['cat_name']
                    end
                  end
                  item['original_cid']=item['cid']
                  item.delete('cid')
                  unless item['props'].nil?
                    item['props_str']=transfer_prop_to_str_v2(dao,item)
                  end

                  sleep 1.2
                  tbk = get_taobaoke_detail(dao,nick,id)
                  unless tbk.nil?
                    item['taobaoke_id']=tbk[:tbk_id] unless tbk[:tbk_id].nil?
                    item['u_id'] = tbk[:user_id] unless tbk[:user_id].nil?
                  end
                  dao.query(dao.build_sql(item))
                  dao.query("update tao_items set taobaoke_id=(select id from taoke_items where num_iid=#{id}) where num_iid=#{id}")
                  dao.query("update tao_items set u_id=(select id from tao_users where nick='#{nick}' limit 1) where num_iid=#{id}")
                  
                  get_item_picture(dao,id,item['item_imgs'])
                  #get_picture_from_description(dao,item['description'],id) unless item['description'].nil?
                end#unless
              rescue Exception => e
                  puts e.message
              end
      end
      def get_item_picture(dao,num_id,imgs)
          rs = dao.query("select id from tao_items where num_iid=#{num_id} ")
          item_id = nil
          if rs.num_rows>0
            rs.each do |row|
              item_id = row[0]
            end
          end 
          unless item_id.nil?
            unless imgs.nil?
              imgs.each do |img|
                next if dao.query("select id from tao_item_pictures where id=#{img['id']}").num_rows>0
                img['item_id']=item_id
                dao.query(dao.build_sql(img))
              end
            end
          end
      end
      def get_taobaoke_detail(dao,nick,num_ids)
            begin
              rs = dao.query("select id from taoke_items where num_iid=#{num_ids}")
              result ={}
              user_id = get_user_info(nick,dao)
              if rs.num_rows>0
                row = rs.fetch_row
                result[:tbk_id]=row[0]
                result[:user_id]=user_id unless user_id.nil?
                return result
              end
              convert_items = TaobaoAPI.get_with(:method=>'taobao.taobaoke.items.convert',
                                       :fields=>'iid,num_iid,click_url,commission,commission_rate,shop_click_url,seller_credit_score',
                                       :num_iids=>num_ids,
                                       :nick=>'kqy929')
              if convert_items['error_response']
                puts convert_items['error_response']
                return nil
              elsif  convert_items['taobaoke_items_convert_response']
                    ah = {}
                    c_items = convert_items['taobaoke_items_convert_response']['taobaoke_items']['taobaoke_item'] unless convert_items['taobaoke_items_convert_response']['taobaoke_items'].nil?
                    c_items.each do |ci|
                      result[:user_id]=user_id unless user_id.nil?
                      if ci.class =='hash'
                          ci['table'] ='taoke_items'
                          escape(ci['title'])
                          ci['created_at']=TaobaoAPI.timestamp
                          ci['updated_at']=TaobaoAPI.timestamp
                          ci['status']=0
                          dao.query(dao.build_sql(ci))
                          return result
                       else
                          ah[ci[0].to_s]=ci[1].to_s
                      end
                    end
                    if ah.keys.length>0
                      ah['table'] ='taoke_items'
                      ah['created_at']=TaobaoAPI.timestamp
                      ah['updated_at']=TaobaoAPI.timestamp
                      ah['status']=0
                      dao.query(dao.build_sql(ah)) 
                      return result
                    end
              end#elseif
            rescue Exception => e
              raise e
            end    
      end
     def get_user_info(nick,dao)           
              begin
                userinfo = dao.query("select id from tao_users where nick='#{nick}'")
                if userinfo.num_rows>0
                  row = userinfo.fetch_row
                    return row[0]
                end
                sleep 0.5
                user = TaobaoAPI.get_with(:method=>'taobao.user.get',
                                        :fields=>'user_id,nick,sex,seller_credit,location,created',
                                        :nick=>nick)
                unless user.nil?
                  u = user['user_get_response']['user']
                  u['table']='tao_users'
                  u['level']=u['seller_credit']['level']
                  u['good_num']=u['seller_credit']['good_num']
                  u['score']=u['seller_credit']['score']
                  u['total_num']=u['seller_credit']['total_num']
                  u['t_user_id']=u['user_id']
                  u['location'] = u['location']['city'] unless u['location'].nil?
                  u['created_at']=TaobaoAPI.timestamp
                  u['updated_at']=TaobaoAPI.timestamp
                  u.delete('user_id')
                  u.delete('seller_credit')
                  u.delete('sex')
                  dao.query(dao.build_sql(u))
                end#unless user.nil?                        
              rescue Exception => e
                raise e
              end
         end
end#class

=begin
begin
db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
tao_dao = DBHelp::DBHelp.out_initialize(db_config1)
o = GetData.new

rs = tao_dao.query("select num_iid,nick,id from spider_items where status=0 order by id desc")
if rs.num_rows>0
  rs.each do |row|
    check = tao_dao.query("select id from tao_items where num_iid=#{row[0]}")
    next if check.nil?
    next if check.num_rows>0
    o.get_item(tao_dao,row[0],row[1],row[2])
  end
end
dao.query("update spider_items s inner join tao_items t on s.num_iid=t.num_iid set s.status=1")
rescue Exception => e
  puts e.message
end
=end
begin
    db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
    tao_dao = DBHelp::DBHelp.out_initialize(db_config1)
    o = GetData.new
    rs = tao_dao.query("select tt.num_iid,tt.description,tt.id from tao_items tt left join (select distinct item_id from             tao_item_pictures) as t on tt.id=t.item_id
    where t.item_id is null order by tt.id desc")
    puts rs.num_rows
    rs.each do |row|
        o.get_picture_from_description(tao_dao,row[1].to_s,row[0].to_i)
    end
rescue Exception => e
end
